#include "log.h"

// 宏替换后
void test_GlobalLoggerBuilder()
{
    // Log::Logger::ptr logger = Log::LoggerManager::getInstance().getLogger("GlobalLoggerBuilder_logger");
    // logger->debug("%s", "测试");
    // logger->info("%s", "测试");
    // logger->warn("%s", "测试");
    // logger->error("%s", "测试");
    // logger->fatal("%s", "测试");

    // size_t count = 0;
    // while (count < 5000)
    // {
    //     logger->fatal("%s-%d", "测试", count++);
    // }

    // ----------------------------------------------------------------------------
    // 测试默认日志器
    DEBUG("%s", "测试");
    INFO("%s", "测试");
    WARN("%s", "测试");
    ERROR("%s", "测试");
    FATAL("%s", "测试");

    size_t count = 0;
    while (count < 5000)
    {
        FATAL("%s-%d", "测试", count++);
    }
}

// void test_GlobalLoggerBuilder()
// {
//     Log::Logger::ptr logger = Log::LoggerManager::getInstance().getLogger("GlobalLoggerBuilder_logger");
//     logger->debug(__FILE__, __LINE__, "%s", "测试");
//     logger->info(__FILE__, __LINE__, "%s", "测试");
//     logger->warn(__FILE__, __LINE__, "%s", "测试");
//     logger->error(__FILE__, __LINE__, "%s", "测试");
//     logger->fatal(__FILE__, __LINE__, "%s", "测试");

//     size_t count = 0;
//     while (count < 5000)
//     {
//         logger->fatal(__FILE__, __LINE__, "%s-%d", "测试", count++);
//     }
// }

int main()
{
    // 测试 util.hpp Log::util::File::createDirectory 是否能创建目录
    // Log::util::File::createDirectory("a/b/c/d/e");

    // 测试format.hpp Log::Formatter::parsePattern() 是否能成功解析格式化字符串
    // 测试message.hpp 值是否转换正确
    // Log::LogMessage msg(Log::LogLevel::level::DEBUG, __LINE__, __FILE__, "logger", "测试");
    // Log::Formatter fmt("abcde%%%%%xxx%[%d{%H:%M:%S}][%t][%c][%f:%l%x]%xx[%p]%T%m%x%x%%");
    // std::string str = fmt.format(msg);
    // std::cout << str << std::endl;

    // ----------------------------------------------------------------------------------------

    // 测试sink.hpp
    // Log::LogMessage msg(Log::LogLevel::level::DEBUG, __LINE__, __FILE__, "logger", "测试");
    // Log::Formatter fmt;
    // std::string str = fmt.format(msg);

    // 标准输出sink测试
    // auto stdout_sink = Log::SinkFactory::create<Log::StdoutSink>();
    // stdout_sink->log(str.c_str(), str.size());

    // 普通文件sink测试
    // auto file_sink = Log::SinkFactory::create<Log::FileSink>("./a/b/c.log");
    // file_sink->log(str.c_str(), str.size());

    // 按大小滚动文件sink测试
    // auto rollbysize_sink = Log::SinkFactory::create<Log::RollBySizeSink>("a/b", 1024);
    // size_t cur_size = 0, cnt = 1;
    // while(cur_size <= 1024)
    // {
    //     std::string tmp = str + std::to_string(cnt ++);
    //     rollbysize_sink->log(tmp.c_str(), tmp.size());
    //     cur_size += tmp.size();
    // }

    // 按时间滚动文件sink测试
    // auto rollbytime_sink = Log::SinkFactory::create<Log::RollByTimeSink>("a/b", 1);
    // rollbytime_sink->log(str.c_str(), str.size());
    // int cnt = 10;
    // while(cnt --)
    // {
    //     std::this_thread::sleep_for(std::chrono::milliseconds(1000));    // 休眠1秒
    //     rollbytime_sink->log(str.c_str(), str.size());
    // }

    // ----------------------------------------------------------------------------------------
    // 测试logger.hpp

    // 测试SyncLogger
    // std::string logger_name = "sycn_logger";
    // Log::LogLevel::level limit_level = Log::LogLevel::level::WARN;
    // Log::Formatter::ptr fmt(new Log::Formatter());
    // Log::LogSink::ptr stdout_lsp = Log::SinkFactory::create<Log::StdoutSink>();
    // Log::LogSink::ptr file_lsp = Log::SinkFactory::create<Log::FileSink>("a/b.log");
    // Log::LogSink::ptr rollBySize_lsp = Log::SinkFactory::create<Log::RollBySizeSink>("c/d", 1024);
    // Log::LogSink::ptr rollByTime_lsp = Log::SinkFactory::create<Log::RollByTimeSink>("e/f", 1);
    // std::vector<Log::LogSink::ptr> sinks = {stdout_lsp, file_lsp, rollBySize_lsp, rollByTime_lsp};
    // Log::Logger::ptr logger(new Log::SyncLogger(logger_name, limit_level, fmt, sinks));
    // std::string str = "测试日志-";

    // logger->debug(__FILE__, __LINE__, "%s", str.c_str());
    // logger->warn(__FILE__, __LINE__, "%s", str.c_str());
    // logger->error(__FILE__, __LINE__, "%s", str.c_str());
    // logger->info(__FILE__, __LINE__, "%s", str.c_str());

    // size_t cursize = 0, cnt = 0;
    // while (cursize < 1024 * 10)
    // {
    //     std::string tmp = str + std::to_string(cnt ++);
    // // vasprintf中参数fmt指的是c风格的字符串，传参要将string转char*
    //     logger->fatal(__FILE__, __LINE__, "%s", tmp.c_str());
    //     cursize += tmp.size();
    // }

    // ---------------------------------------------------------------------
    // 测试logger.hpp中的LoggerBuilder
    // Log::LoggerBuilder *builder = new Log::LocalLoggerBuilder();
    // builder->buildLoggerName("logger");
    // builder->buildLoggerLevel(Log::LogLevel::level::WARN);
    // builder->buildLoggerType(Log::LoggerType::LOGGER_SYNC);
    // builder->buildSink<Log::StdoutSink>();
    // builder->buildSink<Log::FileSink>("a/b.log");
    // Log::Logger::ptr logger = builder->build();
    // logger->debug(__FILE__, __LINE__, "测试");
    // logger->warn(__FILE__, __LINE__, "测试");

    // ----------------------------------------------------------------------
    // 测试buffer.hpp Buffer    使用 md5sum 指令 计算和验证文件的 MD5 哈希值，可以判断两个文件数据是否完全一样
    // Log::util::File::createDirectory("a/b");
    // std::ofstream ofs("./a/b/text.log", std::ios::binary);
    // std::string s;
    // s.resize(10 * 1024 * 1024);
    // for (char& c : s)   c = '1';
    // ofs.write(&s[0], s.size());

    // std::ofstream ofs2("./a/b/text.tmp", std::ios::binary);
    // Log::Buffer buffer;
    // for (char& c : s)   buffer.push(&c, 1);
    // // 下面循环不能直接使用buffer.readAbleSize()，
    // // 因为buffer.readAbleSize()随着buffer._reader_idx的增大(buffer.moveReader())而减小
    // int rsize = buffer.readAbleSize();
    // for (int i = 0; i < rsize; ++ i)
    // {
    //     ofs2.write(buffer.begin(), 1);
    //     buffer.moveReader(1);
    // }

    // ----------------------------------------------------------------------------------
    // 异步写日志
    // Log::LoggerBuilder *builder = new Log::LocalLoggerBuilder();
    // builder->buildLoggerName("logger");
    // builder->buildLoggerLevel(Log::LogLevel::level::WARN);
    // builder->buildLoggerType(Log::LoggerType::LOGGER_ASYNC);
    // builder->buildEnableUnSafeAsync();
    // builder->buildSink<Log::StdoutSink>();
    // builder->buildSink<Log::FileSink>("a/b/c.log");
    // builder->buildSink<Log::RollBySizeSink>("a/b/c/d/e", 1024 * 1024);
    // Log::Logger::ptr logger = builder->build();
    // logger->debug(__FILE__, __LINE__, "测试");
    // logger->warn(__FILE__, __LINE__, "测试");

    // size_t cursize = 0, count = 0;
    // Log::LogMessage msg(Log::LogLevel::level::DEBUG, __LINE__, __FILE__, "logger", "");
    // Log::Formatter fmt("[%d{%H:%M:%S}][%t][%c][%f:%l][%p]%T%m");
    // std::string str = fmt.format(msg);
    // std::string s = "测试";
    // while (cursize < 1024 * 1024 * 10) {
    //     std::string tmp = s + std::to_string(count);
    //     logger->fatal(__FILE__, __LINE__, tmp);
    //     count++;
    //     cursize += tmp.size() + str.size();
    // }

    // ----------------------------------------------------------------------------------
    // 测试logger.hpp中的GlobalLoggerBuilder
    // std::unique_ptr<Log::LoggerBuilder> glb(new Log::GlobalLoggerBuilder());
    // glb->buildLoggerName("GlobalLoggerBuilder_logger");
    // glb->buildLoggerLevel(Log::LogLevel::level::WARN);
    // glb->buildLoggerFormatter("[%c]%m%n");
    // glb->buildLoggerType(Log::LoggerType::LOGGER_ASYNC);
    // glb->buildEnableUnSafeAsync();
    // glb->buildSink<Log::StdoutSink>();
    // glb->buildSink<Log::FileSink>("a/b/c.log");
    // glb->buildSink<Log::RollBySizeSink>("a/b/c/d/e", 1024);

    // glb->build();
    // test_GlobalLoggerBuilder();

    // ----------------------------------------------------------------------------------
    // 宏替换后
    std::unique_ptr<Log::LoggerBuilder> glb(new Log::GlobalLoggerBuilder());
    glb->buildLoggerName("GlobalLoggerBuilder_logger");
    glb->buildLoggerLevel(Log::LogLevel::level::WARN);
    glb->buildLoggerType(Log::LoggerType::LOGGER_ASYNC);
    glb->buildEnableUnSafeAsync();
    glb->buildSink<Log::StdoutSink>();
    glb->buildSink<Log::FileSink>("a/b/c.log");
    glb->buildSink<Log::RollBySizeSink>("a/b/c/d/e", 1024);

    glb->build();
    test_GlobalLoggerBuilder();

    // ----------------------------------------------------------------------------------
    return 0;
}